home *** CD-ROM | disk | FTP | other *** search
/ One Click 14 / OneClick14.iso / Ferramentas / Convert XLS to Pdf / xls2pdf_setup.exe / {app} / lib / gs_res.ps < prev    next >
Encoding:
Text File  |  2002-11-19  |  28.9 KB  |  914 lines

  1. %    Copyright (C) 1994, 1996, 1997, 1998, 1999, 2000 Aladdin Enterprises.  All rights reserved.
  2. % This software is provided AS-IS with no warranty, either express or
  3. % implied.
  4. % This software is distributed under license and may not be copied,
  5. % modified or distributed except as expressly authorized under the terms
  6. % of the license contained in the file LICENSE in this distribution.
  7. % For more information about licensing, please refer to
  8. % http://www.ghostscript.com/licensing/. For information on
  9. % commercial licensing, go to http://www.artifex.com/licensing/ or
  10. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. % San Rafael, CA  94903, U.S.A., +1(415)492-9861.
  12.  
  13. % $Id: gs_res.ps,v 1.26 2002/11/18 20:23:18 ghostgum Exp $
  14. % Initialization file for Level 2 resource machinery.
  15. % When this is run, systemdict is still writable,
  16. % but (almost) everything defined here goes into level2dict.
  17.  
  18. level2dict begin
  19.  
  20. (BEGIN RESOURCES) VMDEBUG
  21.  
  22. % We keep track of (global) instances with another entry in the resource
  23. % dictionary, an .Instances dictionary.  For categories with implicit
  24. % instances, the values in .Instances are the same as the keys;
  25. % for other categories, the values are [instance status size].
  26.  
  27. % Note that the dictionary that defines a resource category is stored
  28. % in global VM.  The PostScript manual says that each category must
  29. % manage global and local instances separately.  However, objects in
  30. % global VM other than systemdict can't reference objects in local VM.
  31. % This means that the resource category dictionary, which would otherwise be
  32. % the obvious place to keep track of the instances, can't be used to keep
  33. % track of local instances.  Instead, we define a dictionary in local VM
  34. % called localinstancedict, in which the key is the category name and
  35. % the value is the analogue of .Instances for local instances.
  36.  
  37. % We don't currently implement automatic resource unloading.
  38. % When and if we do, it should be hooked to the garbage collector.
  39. % However, Ed Taft of Adobe says their interpreters don't implement this
  40. % either, so we aren't going to worry about it for a while.
  41.  
  42. currentglobal false setglobal systemdict begin
  43.   /localinstancedict 5 dict
  44.   .forcedef    % localinstancedict is local, systemdict is global
  45. end true setglobal
  46. /.emptydict 0 dict readonly def
  47. setglobal
  48.  
  49. % Resource category dictionaries have the following keys (those marked with
  50. % * are optional):
  51. %    Standard, defined in the Red Book:
  52. %        Category (name)
  53. %        *InstanceType (name)
  54. %        DefineResource
  55. %            <key> <instance> DefineResource <instance>
  56. %        UndefineResource
  57. %            <key> UndefineResource -
  58. %        FindResource
  59. %            <key> FindResource <instance>
  60. %        ResourceStatus
  61. %            <key> ResourceStatus <status> <size> true
  62. %            <key> ResourceStatus false
  63. %        ResourceForAll
  64. %            <template> <proc> <scratch> ResourceForAll -
  65. %        *ResourceFileName
  66. %            <key> <scratch> ResourceFileName <filename>
  67. %    Additional, specific to our implementation:
  68. %        .Instances (dictionary)
  69. %        .LocalInstances
  70. %            - .LocalInstances <dict>
  71. %        .GetInstance
  72. %            <key> .GetInstance <instance> -true-
  73. %            <key> .GetInstance -false-
  74. %        .CheckResource
  75. %            <key> <value> .CheckResource <key> <value> <ok>
  76. %              (or may give an error if not OK)
  77. %        .DoLoadResource
  78. %            <key> .DoLoadResource <key> (may give an error)
  79. %        .LoadResource
  80. %            <key> .LoadResource - (may give an error)
  81. %        .ResourceFile
  82. %            <key> .ResourceFile <file> -true-
  83. %            <key> .ResourceFile <key> -false-
  84. %        .ResourceFileStatus
  85. %            <key> .ResourceFileStatus 2 <vmusage> -true-
  86. %            <key> .ResourceFileStatus -false-
  87. % All the above procedures expect that the top dictionary on the d-stack
  88. % is the resource dictionary.
  89.  
  90. % Define enough of the Category category so we can define other categories.
  91. % The dictionary we're about to create will become the Category
  92. % category definition dictionary.
  93.  
  94. % .findcategory and .resourceexec are only called from within the
  95. % implementation of the resource 'operators', so they doesn't have to worry
  96. % about cleaning up the stack if they fail (the interpreter's stack
  97. % protection machinery for pseudo-operators takes care of this).
  98. /.findcategory {    % <name> .findcategory -
  99.             %   (pushes the category on the dstack)
  100.   /Category findresource begin
  101. } bind def
  102.  
  103. /.resourceexec {    % <key> /xxxResource .resourceexec -
  104.             %   (also pops the category from the dstack)
  105.   load exec end
  106. } bind def
  107.  
  108. % .getvminstance treats instances on disk as undefined.
  109. /.getvminstance {    % <key> .getvminstance <instance> -true-
  110.             % <key> .getvminstance -false-
  111.   .GetInstance {
  112.     dup 1 get 2 ne { true } { pop false } ifelse
  113.   } {
  114.     false
  115.   } ifelse
  116. } bind def
  117.  
  118. 20 dict begin
  119.  
  120.         % Standard entries
  121.  
  122. /Category /Category def
  123. /InstanceType /dicttype def
  124.  
  125. /DefineResource {
  126.     .CheckResource {
  127.       dup /Category 3 index cvlit .growput
  128.       dup [ exch 0 -1 ] exch
  129.       .Instances 4 2 roll put
  130.         % Make the Category dictionary read-only.  We will have to
  131.         % use .forceput / .forcedef later to replace the dummy,
  132.         % empty .Instances dictionary with the real one later.
  133.       readonly
  134.     } {
  135.       /defineresource load /typecheck signalerror
  136.     } ifelse
  137. } bind def
  138. /FindResource        % (redefined below)
  139.     { .Instances exch get 0 get
  140.     } bind def
  141.  
  142.         % Additional entries
  143.  
  144. /.Instances 30 dict def
  145. .Instances /Category [currentdict 0 -1] put
  146.  
  147. /.LocalInstances 0 dict def
  148. /.GetInstance
  149.     { .Instances exch .knownget
  150.     } bind def
  151. /.CheckResource
  152.     { dup gcheck currentglobal and
  153.        { /DefineResource /FindResource /ResourceForAll /ResourceStatus
  154.          /UndefineResource }
  155.        { 2 index exch known and }
  156.       forall
  157.       not { /defineresource load /invalidaccess signalerror } if
  158.       true
  159.     } bind def
  160.  
  161. .Instances end begin    % for the base case of findresource
  162.  
  163. (END CATEGORY) VMDEBUG
  164.  
  165. % Define the resource operators.  We use the "stack protection" feature of
  166. % odef to make sure the stacks are restored properly on an error.
  167. % This requires that the operators not pop anything from the stack until
  168. % they have executed their logic successfully.  We can't make this
  169. % work for resourceforall, because the procedure it executes mustn't see
  170. % the operands of resourceforall on the stack, but we can make it work for
  171. % the others.
  172.  
  173. % findresource is the only operator that needs to bind //Category.
  174. /findresource {        % <key> <category> findresource <instance>
  175.     2 copy dup /Category eq
  176.       { pop //Category 0 get begin } { .findcategory } ifelse
  177.     /FindResource .resourceexec exch pop exch pop
  178. } bind
  179. end        % .Instances of Category
  180. odef
  181.  
  182. /defineresource {    % <key> <instance> <category> defineresource <instance>
  183.     3 copy .findcategory
  184.     currentdict /InstanceType known {
  185.       dup type InstanceType ne {
  186.         dup type /packedarraytype eq InstanceType /arraytype eq and
  187.         not { /defineresource load /typecheck signalerror } if
  188.       } if
  189.     } if
  190.     /DefineResource .resourceexec
  191.     4 1 roll pop pop pop
  192. } bind odef
  193. % We must prevent resourceforall from automatically restoring the stacks,
  194. % because we don't want the stacks restored if proc causes an error or
  195. % executes a 'stop'. On the other hand, resourceforall is defined in the
  196. % PLRM as an operator, so it must have type /operatortype.  We hack this
  197. % by taking advantage of the fact that the interpreter optimizes tail
  198. % calls, so stack protection doesn't apply to the very last token of an
  199. % operator procedure.
  200. /resourceforall1 {    % <template> <proc> <scratch> <category> resourceforall1 -
  201.     dup /Category findresource begin
  202.     /ResourceForAll load
  203.     % Stack: <template> <proc> <scratch> <category> proc
  204.     exch pop        % pop the category
  205.     exec end
  206. } bind def
  207. /resourceforall {    % <template> <proc> <scratch> <category> resourceforall1 -
  208.     //resourceforall1 exec        % see above
  209. } bind odef
  210. /resourcestatus {    % <key> <category> resourcestatus <status> <size> true
  211.             % <key> <category> resourcestatus false
  212.     2 copy .findcategory /ResourceStatus .resourceexec
  213.      { 4 2 roll pop pop true } { pop pop false } ifelse
  214. } bind odef
  215. /undefineresource {    % <key> <category> undefineresource -
  216.     2 copy .findcategory /UndefineResource .resourceexec pop pop
  217. } bind odef
  218.  
  219. % Define the system parameters used for the Generic implementation of
  220. % ResourceFileName.
  221. systemdict begin
  222. currentdict /pssystemparams known not {
  223.   /pssystemparams 10 dict readonly def
  224. } if
  225. pssystemparams begin
  226.   /FontResourceDir (/Resource/Font/) readonly .forcedef    % pssys'params is r-o
  227.   /GenericResourceDir (/Resource/) readonly .forcedef    % pssys'params is r-o
  228.   /GenericResourcePathSep (/) readonly .forcedef    % pssys'params is r-o
  229.   (%diskFontResourceDir) cvn (/Resource/Font/) readonly .forcedef    % pssys'params is r-o
  230.   (%diskGenericResourceDir) cvn (/Resource/) readonly .forcedef    % pssys'params is r-o
  231. end
  232. end
  233.  
  234. % Define the generic algorithm for computing resource file names.
  235. /.rfnstring 100 string def
  236. /.genericrfn        % <key> <scratch> <prefix> .genericrfn <filename>
  237.  { 3 -1 roll //.rfnstring cvs concatstrings exch copy
  238.  } bind def
  239.  
  240. % Define a procedure for making a packed array in local VM.
  241. /.localpackedarray {    % <obj1> ... <objn> <n> .localpackedarray <packedarray>
  242.   .currentglobal false .setglobal 1 index 2 add 1 roll
  243.   packedarray exch .setglobal
  244. } bind def
  245.  
  246. % Define the Generic category.
  247.  
  248. /Generic mark
  249.  
  250.         % Standard entries
  251.  
  252. % We're still running in Level 1 mode, so dictionaries won't expand.
  253. % Leave room for the /Category entry.
  254. /Category null
  255.  
  256. % Implement the body of Generic resourceforall for local, global, and
  257. % external cases.  'args' is [template proc scratch resdict].
  258. /.enumerateresource {    % <key> [- <proc> <scratch>] .enumerateresource -
  259.   1 index type dup /stringtype eq exch /nametype eq or {
  260.     exch 1 index 2 get cvs exch
  261.   } if
  262.     % Use .setstackprotect to prevent the stacks from being restored if
  263.     % an error occurs during execution of proc.
  264.   1 get false .setstackprotect exec true .setstackprotect
  265. } bind def
  266. /.localresourceforall {        % <key> <value> <args> .localr'forall -
  267.   exch pop
  268.   2 copy 0 get .stringmatch { .enumerateresource } { pop pop } ifelse
  269. } bind def
  270. /.globalresourceforall {    % <key> <value> <args> .globalr'forall -
  271.   exch pop
  272.   2 copy 0 get .stringmatch {
  273.     dup 3 get begin .LocalInstances end 2 index known not {
  274.       .enumerateresource
  275.     } if
  276.   } {
  277.     pop pop
  278.   } ifelse
  279. } bind def
  280. /.externalresourceforall {    % <filename> <len> <args> .externalr'forall -
  281.   3 1 roll 1 index length 1 index sub getinterval exch
  282.   dup 3 get begin .Instances .LocalInstances end
  283.         % Stack: key args insts localinsts
  284.   3 index known {
  285.     pop pop pop
  286.   } {
  287.     2 index known { pop pop } { .enumerateresource } ifelse
  288.   } ifelse
  289. } bind def
  290.  
  291. /DefineResource {
  292.     .CheckResource
  293.        { dup [ exch 0 -1 ]
  294.             % Stack: key value instance
  295.          currentglobal
  296.           { false setglobal 2 index UndefineResource    % remove local def if any
  297.         true setglobal
  298.         .Instances dup //.emptydict eq {
  299.           pop 3 dict
  300.             % As noted above, Category dictionaries are read-only,
  301.             % so we have to use .forcedef here.
  302.           /.Instances 1 index .forcedef    % Category dict is read-only
  303.         } if
  304.           }
  305.           { .LocalInstances dup //.emptydict eq
  306.              { pop 3 dict localinstancedict Category 2 index put
  307.          }
  308.         if
  309.           }
  310.          ifelse
  311.             % Stack: key value instance instancedict
  312.          3 index 2 index .growput
  313.             % Now make the resource value read-only.
  314.          0 2 copy get { readonly } .internalstopped pop
  315.          dup 4 1 roll put exch pop exch pop
  316.        }
  317.        { /defineresource load /typecheck signalerror
  318.        }
  319.     ifelse
  320. } .bind executeonly        % executeonly to prevent access to .forcedef
  321. /UndefineResource
  322.     {  { dup 2 index .knownget
  323.           { dup 1 get 1 ge
  324.          { dup 0 null put 1 2 put pop pop }
  325.          { pop exch .undef }
  326.         ifelse
  327.           }
  328.           { pop pop
  329.           }
  330.          ifelse
  331.        }
  332.       currentglobal
  333.        { 2 copy .Instances exch exec
  334.        }
  335.       if .LocalInstances exch exec
  336.     } bind
  337. % Because of some badly designed code in Adobe's CID font downloader that
  338. % makes findresource and resourcestatus deliberately inconsistent with each
  339. % other, the default FindResource must not call ResourceStatus if there is
  340. % an instance of the desired name already defined in VM.
  341. /FindResource {
  342.     dup .getvminstance {
  343.       exch pop 0 get
  344.     } {
  345.       dup ResourceStatus {
  346.         pop 1 gt {
  347.           .DoLoadResource .getvminstance not {
  348.         /findresource load /undefinedresource signalerror
  349.           } if 0 get
  350.         } {
  351.           .GetInstance pop 0 get
  352.         } ifelse
  353.       } {
  354.        /findresource load /undefinedresource signalerror
  355.       } ifelse
  356.     } ifelse
  357. } bind
  358. % Because of some badly designed code in Adobe's CID font downloader, the
  359. % definition of ResourceStatus for Generic and Font must be the same (!).
  360. % We patch around this by using an intermediate .ResourceFileStatus procedure.
  361. /ResourceStatus {
  362.     dup .GetInstance {
  363.       exch pop dup 1 get exch 2 get true
  364.     } {
  365.       .ResourceFileStatus
  366.     } ifelse
  367. } bind
  368. /.ResourceFileStatus {
  369.     .ResourceFile { closefile 2 -1 true } { pop false } ifelse
  370. } bind
  371. /ResourceForAll {
  372.         % **************** Doesn't present instance groups in
  373.         % **************** the correct order yet.
  374.         % Construct a new procedure to hold the arguments.
  375.         % All objects constructed here must be in local VM to avoid
  376.         % a possible invalidaccess.
  377.     currentdict 4 .localpackedarray    % [template proc scratch resdict]
  378.         % We must pop the resource dictionary off the dict stack
  379.         % when doing the actual iteration, and restore it afterwards.
  380.     .currentglobal not {
  381.       .LocalInstances length 0 ne {
  382.         % We must do local instances, and do them first.
  383.         //.localresourceforall {exec} 0 get 3 .localpackedarray cvx
  384.         .LocalInstances exch {forall} 0 get 1 index 0 get
  385.         currentdict end 3 .execn begin
  386.       } if
  387.     } if
  388.         % Do global instances next.
  389.     //.globalresourceforall {exec} 0 get 3 .localpackedarray cvx
  390.     .Instances exch cvx {forall} 0 get 1 index 0 get
  391.     currentdict end 3 .execn begin
  392.     currentdict /ResourceFileName known {
  393.         % Finally, do instances stored on files.
  394.       dup 0 get 100 string ResourceFileName
  395.       dup length 2 index 0 get length sub 3 -1 roll
  396.       //.externalresourceforall {exec} 0 get 4 .localpackedarray cvx
  397.       100 string {filenameforall} 0 get
  398.       currentdict end 2 .execn begin null    % for pop
  399.     } if pop
  400. } bind
  401. /ResourceFileName
  402.     { /GenericResourceDir getsystemparam 
  403.       Category .namestring concatstrings
  404.       /GenericResourcePathSep getsystemparam concatstrings
  405.       .genericrfn
  406.     } bind
  407.  
  408.         % Additional entries
  409.  
  410. % Unfortunately, we can't create the real .Instances dictionary now,
  411. % because if someone copies the Generic category (which pp. 95-96 of the
  412. % 2nd Edition Red Book says is legitimate), they'll wind up sharing
  413. % the .Instances.  Instead, we have to create .Instances on demand,
  414. % just like the entry in localinstancedict.
  415. % We also have to prevent anyone from creating instances of Generic itself.
  416. /.Instances //.emptydict
  417.  
  418. /.LocalInstances
  419.     { localinstancedict Category .knownget not { //.emptydict } if
  420.     } bind
  421. /.GetInstance
  422.     { currentglobal
  423.        { .Instances exch .knownget }
  424.        { .LocalInstances 1 index .knownget
  425.           { exch pop true }
  426.           { .Instances exch .knownget }
  427.          ifelse
  428.        }
  429.       ifelse
  430.     } bind
  431. /.CheckResource
  432.     { true
  433.     } bind
  434. /.vmused {
  435.         % - .vmused <usedvalue>
  436.         % usedvalue = vmstatus in global + vmstatus in local.
  437.   0 2 {
  438.     .currentglobal not .setglobal
  439.     vmstatus pop exch pop add
  440.   } repeat
  441. } bind def
  442. /.DoLoadResource {
  443.         % .LoadResource may push entries on the operand stack.
  444.         % It is an undocumented feature of Adobe implementations,
  445.         % which we must match for the sake of some badly written
  446.         % font downloading code, that such entries are popped
  447.         % automatically.
  448.     count 1 index cvlit .vmused
  449.         % Stack: key count litkey memused
  450.     {.LoadResource} 4 1 roll 4 .execn
  451.         % Stack: ... count key memused
  452.     .vmused exch sub
  453.     1 index .getvminstance not {
  454.       pop dup /undefinedresource signalerror    % didn't load
  455.     } if
  456.     dup 1 1 put
  457.     2 3 -1 roll put
  458.         % Stack: ... count key
  459.     exch count 1 sub exch sub {exch pop} repeat
  460. } bind
  461. /.LoadResource
  462.     { dup .ResourceFile
  463.        { exch pop currentglobal
  464.           { .runresource }
  465.           { true setglobal { .runresource } stopped false setglobal { stop } if }
  466.          ifelse
  467.        }
  468.        { dup /undefinedresource signalerror
  469.        }
  470.      ifelse
  471.     } bind
  472. /.ResourceFile
  473.     { currentdict /ResourceFileName known
  474.        { mark 1 index 100 string { ResourceFileName }
  475.          .internalstopped
  476.           { cleartomark false }
  477.           { exch pop findlibfile
  478.          { exch pop exch pop true }
  479.          { pop false }
  480.         ifelse
  481.           }
  482.          ifelse
  483.        }
  484.        { false }
  485.       ifelse
  486.     } bind
  487.  
  488. .dicttomark
  489. /Category defineresource pop
  490.  
  491. % Fill in the rest of the Category category.
  492. /Category /Category findresource dup
  493. /Generic /Category findresource begin {
  494.   /FindResource /ResourceForAll /ResourceStatus /.ResourceFileStatus
  495.   /UndefineResource /ResourceFileName
  496.   /.ResourceFile /.LoadResource /.DoLoadResource
  497. } { dup load put dup } forall
  498. pop readonly pop end
  499.  
  500. (END GENERIC) VMDEBUG
  501.  
  502. % Define the fixed categories.
  503.  
  504. mark
  505.     % Non-Type categories with existing entries.
  506.  /ColorSpaceFamily
  507.    { }    % These must be deferred, because optional features may add some.
  508.  /Emulator
  509.    mark EMULATORS { cvn } forall .packtomark
  510.  /Filter
  511.    { }    % These must be deferred, because optional features may add some.
  512.  /IODevice
  513.     % Loop until the .getiodevice gets a rangecheck.
  514.    errordict /rangecheck 2 copy get
  515.    errordict /rangecheck { pop stop } put    % pop the command
  516.    mark 0 { {
  517.     dup .getiodevice dup null eq { pop } { exch } ifelse 1 add
  518.    } loop} .internalstopped
  519.    pop pop pop .packtomark
  520.    4 1 roll put
  521.    .clearerror
  522.     % Type categories listed in the Red Book.
  523.  /ColorRenderingType
  524.    { }    % These must be deferred, because optional features may add some.
  525.  /FMapType
  526.    { }    % These must be deferred, because optional features may add some.
  527.  /FontType
  528.    { }    % These must be deferred, because optional features may add some.
  529.  /FormType
  530.    { }    % These must be deferred, because optional features may add some.
  531.  /HalftoneType
  532.    { }    % These must be deferred, because optional features may add some.
  533.  /ImageType
  534.    { }    % Deferred, optional features may add some.
  535.  /PatternType
  536.    { }  % Deferred, optional features may add some.
  537.     % Type categories added since the Red Book.
  538.  /setsmoothness where {
  539.    pop /ShadingType { }    % Deferred, optional features may add some.
  540.  } if
  541. counttomark 2 idiv
  542.  { mark
  543.  
  544.         % Standard entries
  545.  
  546.         % We'd like to prohibit defineresource,
  547.         % but because optional features may add entries, we can't.
  548.         % We can at least require that the key and value match.
  549.    /DefineResource
  550.     { currentglobal not
  551.        { /defineresource load /invalidaccess signalerror }
  552.        { 2 copy ne
  553.           { /defineresource load /rangecheck signalerror }
  554.           { dup .Instances 4 -2 roll .growput }
  555.          ifelse
  556.        }
  557.       ifelse
  558.     } bind
  559.    /UndefineResource
  560.     { /undefineresource load /invalidaccess signalerror } bind
  561.    /FindResource
  562.     { .Instances 1 index .knownget
  563.        { exch pop }
  564.        { /findresource load /undefinedresource signalerror }
  565.       ifelse
  566.     } bind
  567.    /ResourceStatus
  568.     { .Instances exch known { 0 0 true } { false } ifelse } bind
  569.    /ResourceForAll
  570.     /Generic /Category findresource /ResourceForAll get
  571.  
  572.         % Additional entries
  573.  
  574.    counttomark 2 add -1 roll
  575.    dup length dict dup begin exch { dup def } forall end
  576.         % We'd like to make the .Instances readonly here,
  577.         % but because optional features may add entries, we can't.
  578.    /.Instances exch
  579.    /.LocalInstances    % used by ResourceForAll
  580.     0 dict def
  581.  
  582.    .dicttomark /Category defineresource pop
  583.  } repeat pop
  584.  
  585. (END FIXED) VMDEBUG
  586.  
  587. % Define the other built-in categories.
  588.  
  589. /.definecategory    % <name> -mark- <key1> ... <valuen> .definecategory -
  590.  { counttomark 2 idiv 2 add        % .Instances, Category
  591.    /Generic /Category findresource dup maxlength 3 -1 roll add
  592.    dict .copydict begin
  593.    counttomark 2 idiv { def } repeat pop    % pop the mark
  594.    currentdict end /Category defineresource pop
  595.  } bind def
  596.  
  597. /ColorRendering mark /InstanceType /dicttype .definecategory
  598. % ColorSpace is defined below
  599. % Encoding is defined below
  600. % Font is defined below
  601. /Form mark /InstanceType /dicttype .definecategory
  602. /Halftone mark /InstanceType /dicttype .definecategory
  603. /Pattern mark /InstanceType /dicttype .definecategory
  604. /ProcSet mark /InstanceType /dicttype .definecategory
  605. % Added since the Red Book:
  606. /ControlLanguage mark /InstanceType /dicttype .definecategory
  607. /HWOptions mark /InstanceType /dicttype .definecategory
  608. /Localization mark /InstanceType /dicttype .definecategory
  609. /OutputDevice mark /InstanceType /dicttype .definecategory
  610. /PDL mark /InstanceType /dicttype .definecategory
  611. % CIDFont, CIDMap, and CMap are defined in gs_cidfn.ps
  612. % FontSet is defined in gs_cff.ps
  613. % IdiomSet is defined in gs_ll3.ps
  614. % InkParams and TrapParams are defined in gs_trap.ps
  615.  
  616. (END MISC) VMDEBUG
  617.  
  618. % Define the ColorSpace category.
  619.  
  620. /.defaultcsnames mark
  621.   /DefaultGray 0
  622.   /DefaultRGB 1
  623.   /DefaultCMYK 2
  624. .dicttomark readonly def
  625.  
  626. % The "hooks" are no-ops here, redefined in LL3.
  627. /.definedefaultcs {    % <index> <value> .definedefaultcs -
  628.   pop pop
  629. } bind def
  630. /.undefinedefaultcs {    % <index> .undefinedefaultcs -
  631.   pop
  632. } bind def
  633.  
  634. /ColorSpace mark
  635.  
  636. /InstanceType /arraytype
  637.  
  638. % We keep track of whether there are any local definitions for any of
  639. % the Default keys.  This information must get saved and restored in
  640. % parallel with the local instance dictionary, so it must be stored in
  641. % local VM.
  642. userdict /.localcsdefaults false put
  643.  
  644. /DefineResource {
  645.   2 copy /Generic /Category findresource /DefineResource get exec
  646.   exch pop
  647.   exch //.defaultcsnames exch .knownget {
  648.     1 index .definedefaultcs
  649.     currentglobal not { .userdict /.localcsdefaults true put } if
  650.   } if
  651. } bind
  652.  
  653. /UndefineResource {
  654.   dup /Generic /Category findresource /UndefineResource get exec
  655.   //.defaultcsnames 1 index .knownget {
  656.     % Stack: resname index
  657.     currentglobal {
  658.       .undefinedefaultcs pop
  659.     } {
  660.     % We removed the local definition, but there might be a global one.
  661.       exch .GetInstance {
  662.     0 get .definedefaultcs
  663.       } {
  664.     .undefinedefaultcs
  665.       } ifelse
  666.     % Recompute .localcsdefaults by scanning.  This is rarely needed.
  667.       .userdict /.localcsdefaults false //.defaultcsnames {
  668.     pop .LocalInstances exch known { pop true exit } if
  669.       } forall put
  670.     } ifelse
  671.   } {
  672.     pop
  673.   } ifelse
  674. } bind
  675.  
  676. .definecategory            % ColorSpace
  677.  
  678. % Define the Encoding category.
  679.  
  680. /Encoding mark
  681.  
  682. /InstanceType /arraytype
  683.  
  684. % Handle already-registered encodings, including lazily loaded encodings
  685. % that aren't loaded yet.
  686.  
  687. /.Instances mark
  688.   EncodingDirectory
  689.    { dup length 256 eq { [ exch readonly 0 -1 ] } { pop [null 2 -1] } ifelse
  690.    } forall
  691. .dicttomark
  692.  
  693. /.ResourceFileDict mark
  694.   EncodingDirectory
  695.    { dup length 256 eq { pop pop } { 0 get } ifelse
  696.    } forall
  697. .dicttomark
  698.  
  699. /ResourceFileName
  700.  { .ResourceFileDict 2 index .knownget
  701.     { exch copy exch pop }
  702.     { /Generic /Category findresource /ResourceFileName get exec }
  703.    ifelse
  704.  } bind
  705.  
  706. .definecategory            % Encoding
  707.  
  708. % Make placeholders in level2dict for the redefined Encoding operators,
  709. % so that they will be swapped properly when we switch language levels.
  710.  
  711. /.findencoding /.findencoding load def
  712. /findencoding /findencoding load def
  713. /.defineencoding /.defineencoding load def
  714.  
  715. (END ENCODING) VMDEBUG
  716.  
  717. % Define the Font category.
  718.  
  719. /.fontstatus {        % <fontname> .fontstatus <fontname> <found>
  720.   {        % Create a loop context just so we can exit it early.
  721.         % Check Fontmap.
  722.     Fontmap 1 index .knownget {
  723.       {
  724.     dup type /nametype eq {
  725.       .fontstatus { pop null exit } if
  726.     } {
  727.       dup type /stringtype eq {
  728.         findlibfile { closefile pop null exit } if pop
  729.       } {
  730.         % Procedure, assume success.
  731.         pop null exit
  732.       } ifelse
  733.     } ifelse
  734.       } forall dup null eq { pop true exit } if
  735.     } if
  736.         % Convert names to strings; give up on other types.
  737.     dup type /nametype eq { .namestring } if
  738.     dup type /stringtype ne { false exit } if
  739.         % Check the resource directory.
  740.     dup .fonttempstring /FontResourceDir getsystemparam .genericrfn
  741.     status {
  742.       pop pop pop pop true exit
  743.     } if
  744.         % Check for a file on the search path with the same name
  745.         % as the font.
  746.     findlibfile { closefile true exit } if
  747.         % Scan a FONTPATH directory and try again.
  748.     .scannextfontdir not { false exit } if
  749.   } loop
  750. } bind def
  751.  
  752. /Font mark
  753.  
  754. /InstanceType /dicttype
  755.  
  756. /DefineResource
  757.     { 2 copy //definefont exch pop
  758.       /Generic /Category findresource /DefineResource get exec
  759.     } bind
  760. /UndefineResource
  761.     { dup //undefinefont
  762.       /Generic /Category findresource /UndefineResource get exec
  763.     } bind
  764. /FindResource {
  765.     dup .getvminstance {
  766.       exch pop 0 get
  767.     } {
  768.       dup ResourceStatus {
  769.         pop 1 gt { .loadfontresource } { .GetInstance pop 0 get } ifelse
  770.       } {
  771.         .loadfontresource
  772.       } ifelse
  773.     } ifelse
  774. } bind
  775. /ResourceForAll {
  776.     { .scannextfontdir not { exit } if } loop
  777.     /Generic /Category findresource /ResourceForAll get exec
  778. } bind
  779. /.ResourceFileStatus {
  780.     .fontstatus { pop 2 -1 true } { pop false } ifelse
  781. } bind
  782.  
  783. /.loadfontresource {
  784.     dup vmstatus pop exch pop exch
  785.         % Hack: rebind .currentresourcefile so that all calls of
  786.         % definefont will know these are built-in fonts.
  787.     currentfile {pop //findfont exec} .execasresource  % (findfont is a procedure)
  788.     exch vmstatus pop exch pop exch sub
  789.         % stack: name font vmused
  790.         % findfont has the prerogative of not calling definefont
  791.         % in certain obscure cases of font substitution.
  792.     2 index .getvminstance {
  793.       dup 1 1 put
  794.       2 3 -1 roll put
  795.     } {
  796.       pop
  797.     } ifelse exch pop
  798. } bind
  799.  
  800. /.Instances FontDirectory length 2 mul dict
  801.  
  802. .definecategory            % Font
  803.  
  804. % Redefine font "operators".
  805. /.definefontmap
  806.  { /Font /Category findresource /.Instances get
  807.    dup 3 index known
  808.     { pop
  809.     }
  810.     { 2 index
  811.         % Make sure we create the array in global VM.
  812.       .currentglobal true .setglobal
  813.       [null 2 -1] exch .setglobal
  814.       .growput
  815.     }
  816.    ifelse
  817.    //.definefontmap exec
  818.  } bind def
  819.  
  820. % Make sure the old definitions are still in systemdict so that
  821. % they will get bound properly.
  822. systemdict begin
  823.   /.origdefinefont /definefont load def
  824.   /.origundefinefont /undefinefont load def
  825.   /.origfindfont /findfont load def
  826. end
  827. /definefont {
  828.   /Font defineresource
  829. } bind odef
  830. /undefinefont {
  831.   /Font undefineresource
  832. } bind odef
  833. % The Red Book requires that findfont be a procedure, not an operator,
  834. % but it still needs to restore the stacks reliably if it fails.
  835. /.findfontop {
  836.   /Font findresource
  837. } bind odef
  838. /findfont {
  839.   .findfontop
  840. } bind def    % Must be a procedure, not an operator
  841.  
  842. % Remove initialization utilities.
  843. currentdict /.definecategory .undef
  844. currentdict /.emptydict .undef
  845.  
  846. end                % level2dict
  847.  
  848. % Convert deferred resources after we finally switch to Level 2.
  849.  
  850. /.fixresources {
  851.     % Encoding resources
  852.   EncodingDirectory
  853.    { dup length 256 eq
  854.       { /Encoding defineresource pop }
  855.       { pop pop }
  856.      ifelse
  857.    } forall
  858.   /.findencoding { /Encoding findresource } bind def
  859.   /findencoding /.findencoding load def        % must be a procedure
  860.   /.defineencoding { /Encoding defineresource pop } bind def
  861.     % ColorRendering resources and ProcSet
  862.   systemdict /ColorRendering .knownget {
  863.     /ColorRendering exch /ProcSet defineresource pop
  864.     systemdict /ColorRendering undef
  865.     /Default currentcolorrendering /ColorRendering defineresource pop
  866.   } if
  867.     % ColorSpace resources
  868.   systemdict /CIEsRGB .knownget {
  869.     /sRGB exch /ColorSpace defineresource pop
  870.     systemdict /CIEsRGB undef
  871.   } if
  872.     % ColorSpaceFamily resources
  873.   colorspacedict { pop dup /ColorSpaceFamily defineresource pop } forall
  874.     % Filter resources
  875.   filterdict { pop dup /Filter defineresource pop } forall
  876.     % FontType and FMapType resources
  877.   buildfontdict { pop dup /FontType defineresource pop } forall
  878.   mark
  879.     buildfontdict 0 known { 2 3 4 5 6 7 8 } if
  880.     buildfontdict 9 known { 9 } if
  881.   counttomark { dup /FMapType defineresource pop } repeat pop
  882.     % FormType resources
  883.   .formtypes { pop dup /FormType defineresource pop } forall
  884.     % HalftoneType resources
  885.   .halftonetypes { pop dup /HalftoneType defineresource pop } forall
  886.     % ColorRenderingType resources
  887.   .colorrenderingtypes {pop dup /ColorRenderingType defineresource pop} forall
  888.     % ImageType resources
  889.   .imagetypes { pop dup /ImageType defineresource pop } forall
  890.     % PatternType resources
  891.   .patterntypes { pop dup /PatternType defineresource pop } forall
  892.     % Make the fixed resource categories immutable.
  893.   /.shadingtypes where {
  894.     pop .shadingtypes { pop dup /ShadingType defineresource pop } forall
  895.   } if
  896.   [ /ColorSpaceFamily /Emulator /Filter /IODevice /ColorRenderingType
  897.     /FMapType /FontType /FormType /HalftoneType /ImageType /PatternType
  898.     /.shadingtypes where { pop /ShadingType } if
  899.   ] {
  900.     /Category findresource
  901.     dup /.Instances get readonly pop
  902.     .LocalInstances readonly pop
  903.     readonly pop
  904.   } forall
  905.     % clean up
  906.   systemdict /.fixresources undef
  907. } bind def
  908.  
  909. %% Replace 1 (gs_resmp.ps)
  910. (gs_resmp.ps) runlibfile
  911.